package com.svms.service.mapper;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.svms.model.SvmsVehicle;
import com.svms.service.constants.Constants;
import com.svms.service.factory.SvmsServiceFactory;
import com.svms.service.util.NumberGeneratorUtils;
import com.svms.service.vo.TransportVO;
import com.svms.service.vo.VehicleVO;

/**
 * @author om39a
 * 
 */
public final class VehicleMapper {

	private VehicleMapper() {

	}

	public static VehicleVO dmlToService(SvmsVehicle svmsVehicle) {

		VehicleVO vehicleVO = new VehicleVO();
		vehicleVO.setVehicleId(String.valueOf(svmsVehicle.getVehicleId()));
		vehicleVO.setVehicleNumber(String.valueOf(svmsVehicle.getVehicleNo()));
		vehicleVO.setModel(svmsVehicle.getModel());
		vehicleVO.setDistanceTraveled(String.valueOf(svmsVehicle
				.getTotalDistance()));
		vehicleVO.setVehicleDocuments(setvehicleDocs(svmsVehicle));
		setTransport(svmsVehicle, vehicleVO);
		return vehicleVO;
	}

	private static void setTransport(SvmsVehicle svmsVehicle,
			VehicleVO vehicleVO) {
		TransportVO transport = new TransportVO();
		transport.setTransportId(String.valueOf(svmsVehicle.getTransportId()));
		vehicleVO.setTransport(SvmsServiceFactory.getInstance().getTransport()
				.getTransport(transport));
	}

	private static Map<String, Date> setvehicleDocs(SvmsVehicle svmsVehicle) {
		Map<String, Date> vehicleDocs = new HashMap<String, Date>();
		vehicleDocs.put(Constants.FORM47_TD, svmsVehicle.getForm47());
		vehicleDocs.put(Constants.FC_TD, svmsVehicle.getFc());
		vehicleDocs.put(Constants.INSURANCE_TD, svmsVehicle.getInsureance());
		vehicleDocs.put(Constants.TAX_TD, svmsVehicle.getTax());
		vehicleDocs.put(Constants.NC_TD, svmsVehicle.getNc());
		return vehicleDocs;
	}

	/**
	 * @param vehicleVO
	 * @return
	 * @throws Exception
	 *             Number generator Component Error
	 */
	public static SvmsVehicle serviceToDml(VehicleVO vehicleVO)
			throws Exception {

		SvmsVehicle svmsVehicle = new SvmsVehicle();
		svmsVehicle.setVehicleId(Long.valueOf(vehicleVO.getVehicleId()));
		svmsVehicle.setCreateTime(new Date());
		svmsVehicle.setFc(vehicleVO.getVehicleDocuments().get(Constants.FC_TD));
		svmsVehicle.setInsureance(vehicleVO.getVehicleDocuments().get(
				Constants.INSURANCE_TD));
		svmsVehicle.setTax(vehicleVO.getVehicleDocuments()
				.get(Constants.TAX_TD));
		svmsVehicle.setNc(vehicleVO.getVehicleDocuments().get(Constants.NC_TD));
		svmsVehicle.setForm47(vehicleVO.getVehicleDocuments().get(
				Constants.FORM47_TD));
		svmsVehicle.setModel(vehicleVO.getModel().trim());
		svmsVehicle.setTotalDistance(Integer.parseInt(vehicleVO
				.getDistanceTraveled().trim()));
		svmsVehicle.setTransportId(Long.parseLong(vehicleVO.getTransport()
				.getTransportId().trim()));
		svmsVehicle.setUpdateBy("admin");
		svmsVehicle.setUpdatedTime(new Date());
		svmsVehicle.setVehicleNo(vehicleVO.getVehicleNumber().trim());

		return svmsVehicle;
	}

}
